package com.linkage.educloud.ah.view.fragment.topic;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.adapter.topic.TopicChildListAdapter;
import com.linkage.educloud.ah.bean.LabelBean;
import com.linkage.educloud.ah.bean.TopicChildBean;
import com.linkage.educloud.ah.evenbus.TopicEvent;
import com.linkage.educloud.ah.mvp.contract.topic.ITopicChildContract;
import com.linkage.educloud.ah.mvp.presenter.topic.TopicChildPresenter;
import com.linkage.educloud.ah.view.BasicFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Mr'Tang on 2018/11/7.
 */

public class TopicChildListFragment extends BasicFragment implements ITopicChildContract.IView {

    @BindView(R.id.common_recylerview)
    XRecyclerView common_recylerview;

    List<TopicChildBean> list = new ArrayList<>();
    TopicChildListAdapter adapter;
    int page = 1;

    public static String KEY_NAME = "name";
    public static String KEY_SORT = "sort";
    public static String KEY_ID = "id";

    ITopicChildContract.IPresenter iPresenter;

    public static TopicChildListFragment newInstance(String name, String sort, String topic_id) {
        TopicChildListFragment fragment = new TopicChildListFragment();
        Bundle args = new Bundle();
        args.putString(KEY_NAME, name);
        args.putString(KEY_SORT, sort);
        args.putString(KEY_ID, topic_id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.common_one_list;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        iPresenter = new TopicChildPresenter(getActivity(), this);
        initRecylerview();
        iPresenter.getCards(getArguments().getString(KEY_ID), "", getArguments().getString(KEY_SORT), getArguments().getString(KEY_NAME).equals("全部") ? "" : getArguments().getString(KEY_NAME), page);
        EventBus.getDefault().register(this);
    }

    private void initRecylerview() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        common_recylerview.setLayoutManager(layoutManager);
        common_recylerview.setItemAnimator(new DefaultItemAnimator());
        common_recylerview.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        common_recylerview.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);

        common_recylerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                iPresenter.getCards(getArguments().getString(KEY_ID), "", getArguments().getString(KEY_SORT), getArguments().getString(KEY_NAME).equals("全部") ? "" : getArguments().getString(KEY_NAME), page);
            }

            @Override
            public void onLoadMore() {
                page++;
                iPresenter.getCards(getArguments().getString(KEY_ID), "", getArguments().getString(KEY_SORT), getArguments().getString(KEY_NAME).equals("全部") ? "" : getArguments().getString(KEY_NAME), page);
            }
        });

        adapter = new TopicChildListAdapter(getActivity(), list);
        common_recylerview.setAdapter(adapter);
    }

    public void initRecylerviewDate(List<TopicChildBean> mlist) {
        if (mlist.size() < 10) {
            common_recylerview.setLoadingMoreEnabled(false);
        } else {
            common_recylerview.setLoadingMoreEnabled(true);
        }
        adapter.addAll(mlist, page != 1);
    }

    public void stopRefresh() {
        common_recylerview.refreshComplete();
        common_recylerview.loadMoreComplete();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(TopicEvent messageEvent) {
        iPresenter.getCards(getArguments().getString(KEY_ID), "", String.valueOf(messageEvent.getMessage()), getArguments().getString(KEY_NAME).equals("全部") ? "" : getArguments().getString(KEY_NAME), page);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    //无用 和child共有一个contract的
    @Override
    public void initLabelDate(List<LabelBean> list) {

    }
}
